﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace HL.IconPro.MVVM.ViewModels
{
    class WelcomeWindowViewModel : Model
    {
        #region Constructors
        public WelcomeWindowViewModel() { }
        #endregion

        #region Destructors

        #endregion

        #region Fields

        #endregion

        #region Properties

        #endregion

        #region Procedures
        private void IconFromStaticImage(WelcomeWindow Source)
        {
        entry: Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Portable Network Graphics (*.png)|*png";
            dialog.CheckFileExists = true;
            if (dialog.ShowDialog(Source) == false) return;
            CreateIconFromImageViewModel mwvm = null;
            System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Open);
            System.Windows.Media.Imaging.BitmapFrame frame;
            System.Windows.Media.Imaging.BitmapDecoder dec =
                System.Windows.Media.Imaging.BitmapDecoder.Create(fs, System.Windows.Media.Imaging.BitmapCreateOptions.None, System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
            frame = dec.Frames[0];
            fs.Close();
            mwvm = new CreateIconFromImageViewModel(frame);
            if (frame.PixelWidth != frame.PixelHeight)
            {
                if (System.Windows.MessageBox.Show("The selected source is not square. Would you like to try another image?",
                     "Invalid Image", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Exclamation) == System.Windows.MessageBoxResult.Yes)
                {
                    goto entry;
                }
            }
            CreateIconFromImage mw = new CreateIconFromImage();
            mw.DataContext = mwvm;
            mw.Show();
            Source.Close();
        }
        private void IconFromFolderSource(WelcomeWindow Source)
        {
            Lib.Wpf.Dialogs.FolderBrowserDialog dialog = new Lib.Wpf.Dialogs.FolderBrowserDialog();
            dialog.Caption = "Select the Source folder:";
            if (dialog.ShowDialog() == false) return;
            string[] pngs = System.IO.Directory.GetFiles(dialog.FolderPath, "*.png");
            MVVM.ViewModels.MainWindowViewModel mwvm = new MainWindowViewModel();
            foreach (string png in pngs)
            {
                    BitmapFrame fr = BitmapFrame.Create(new Uri(png));
                    fr = BitmapFrame.Create(Lib.Wpf.Helpers.GetResized(fr, fr.PixelWidth));
                    mwvm.Frames.Add(new Models.IconFrameModel(fr, fr.Decoder));
            }
            MainWindow mw = new MainWindow();
            mw.DataContext = mwvm;
            mw.Show();
            Source.Close();
        }
        public Command CreateAnEmptyIconCommand
        {
            get
            {
                if (_CreateAnEmptyIconCommand == null)
                {
                    _CreateAnEmptyIconCommand = new Command(new Action<object>((object parameter) =>
                    {
                        MainWindow mw = new MainWindow();
                        mw.Show();
                        ((System.Windows.Window)parameter).Close();
                    }));
                }
                return _CreateAnEmptyIconCommand;
            }
        }
        #endregion

        #region Static

        #endregion

        #region Commands
        private Command _ExtractIconCommand;
        private Command _CreateAnIconFromImageCommand;
        private Command _CreateAnEmptyIconCommand;
        private Command _OpenAnIconFileCommand;
        private Command _NothingCommand;

        public Command ExtractIconCommand
        {
            get
            {
                if (_ExtractIconCommand == null)
                {
                    _ExtractIconCommand = new Command(new Action<object>((object parameter) =>
                    {
                        Lib.Wpf.Dialogs.IconBrowserDialog dialog = new Lib.Wpf.Dialogs.IconBrowserDialog();
                        if (dialog.ShowDialog((System.Windows.Window)parameter) == true)
                        {
                            MainWindowViewModel mwvm = new MainWindowViewModel();
                            if(!dialog.FileName.ToLower().EndsWith(".dll") && !dialog.FileName.EndsWith(".exe"))
                            {
                                System.Windows.MessageBox.Show("Please choose icons from EXE and DLL files only!",
                                    "Invalid Icon Source", System.Windows.MessageBoxButton.OK,
                                     System.Windows.MessageBoxImage.Error);
                                return;
                            }
                            System.IO.Stream source= Lib.Wpf.Tools.PEIconExtractor.ExtractIcon(dialog.FileName, dialog.Index);
                            if(source == null)
                            {
                                System.Windows.MessageBox.Show("The selected icon was empty!?",
                                    "Invalid Icon Source", System.Windows.MessageBoxButton.OK,
                                     System.Windows.MessageBoxImage.Error);
                                return;
                            }
                            mwvm.OpenIcon(source);
                            MainWindow mw = new MainWindow();
                            mw.DataContext = mwvm;
                            mw.Show();
                            ((System.Windows.Window)parameter).Close();
                        }
                    }));
                }
                return _ExtractIconCommand;
            }
        }
        public Command CreateAnIconFromImageCommand
        {
            get
            {
                if (_CreateAnIconFromImageCommand == null)
                {
                    _CreateAnIconFromImageCommand = new Command(new Action<object>((object parameter) =>
                    {
                        if (!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl))
                            IconFromStaticImage((WelcomeWindow)parameter);
                        else
                            IconFromFolderSource((WelcomeWindow)parameter);
                    }));
                }
                return _CreateAnIconFromImageCommand;
            }
        }

        public Command OpenAnIconFileCommand
        {
            get
            {
                if (_OpenAnIconFileCommand == null)
                {
                    _OpenAnIconFileCommand = new Command(new Action<object>((object parameter) =>
                    {
                        Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
                        dialog.Filter = "Icon File(*.ico)|*.ico";
                        dialog.CheckFileExists = true;
                        if (dialog.ShowDialog() == false) return;
                        MainWindowViewModel mwvm = new MainWindowViewModel();
                        System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Open);
                        mwvm.OpenIcon(fs);
                        fs.Close();
                        MainWindow mw = new MainWindow();
                        mw.DataContext = mwvm;
                        mw.Show();
                        ((System.Windows.Window)parameter).Close();
                    }));
                }
                return _OpenAnIconFileCommand;
            }
        }
        public Command NothingCommand
        {
            get
            {
                if (_NothingCommand == null)
                {
                    _NothingCommand = new Command(new Action<object>((object parameter) =>
                    {
                        App.Current.Shutdown();
                    }));
                }
                return _NothingCommand;
            }
        }
        #endregion
    }
}
